# file:    AppleScript.rb
# author:  davidfmiller http://www.fivevoltlogic.com
# about:   a class that lets you execute AppleScript through Ruby
#
# history: v0.1 ??
#          - beta release
#
# notes:   AppleScript errors will be echoed to the terminal instead of being captured in the result
#     

class AppleScript

	# create a new AppleScript, specifying either the contents of the script itself
	# (through a String) or a file that contains the contents of the script 
	def initialize(script)
		@cmd = nil;
		if (script.class == String)
			@cmd = script
		elsif (script.class == File)
			@cmd = script
		else
			raise ArgumentError("Argument must either be of type String or File")
		end
	end

	# returns the result (as a string) of executing the AppleScript 
	def run
		path = nil
		if (@cmd.class == String)
			path = File::Separator + 'tmp' + File::Separator + Time.now.to_f.to_s + '.applescript'
			File.open(path, 'w') { |file| file.puts @cmd }
		else
			path = @cmd.path
		end

		# spit command to the screen
#		puts File.new(path).read 

		return `osascript #{path}`
	end	
end